﻿Imports System
Imports System.ComponentModel
Imports Microsoft.VisualBasic

<TypeConverter(GetType(RangeCheckPropertiesConverter))> _
Public Class RangeCheckProperties

   Private _MaxValue As ValueType = Single.MaxValue
   Private _MinValue As ValueType = Single.MinValue
   Private _AllowNegative As Boolean = True
   Private _MinGT As Boolean = False
   Private _MaxLT As Boolean = False

   Friend Parent As NumericInputBox

   Friend Sub New(ByVal Parent As NumericInputBox)
      Me.Parent = Parent
   End Sub

   Friend ReadOnly Property AllowNegative() As Boolean
      Get
         Return _AllowNegative
      End Get
   End Property


   Private _Enabled As Boolean = False

   '<Browsable(False)> _
   <DisplayName("1. Range Checking Enabled")> _
   <NotifyParentProperty(True)> _
   <Description("Enables Range Checking of Input")> _
   Public Property Enabled() As Boolean
      Get
         Return _Enabled
      End Get
      Set(ByVal value As Boolean)
         _Enabled = value
      End Set
   End Property 'Enabled

   <NotifyParentProperty(True)> _
   <Category("Min")> _
   <EditorAttribute("TnTinMn.NIB.Design.NIB_RangeCheckMinMaxPropertiesEditor, TnTinMn.NIB.Design, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", GetType(System.Drawing.Design.UITypeEditor))> _
   <DisplayName("2a. Min Value")> _
   <Description("Minimum allowed value.")> _
   Public Property MinValue() As ValueType
      Get
         Return _MinValue
      End Get

      Set(ByVal value As ValueType)
         If value Is Nothing Then value = 0

         MaxOrMinValueRangeCheck(value, "ValueMin")
         _MinValue = value
         If NumericInputBox.A_IsLessThan_B(_MinValue, 0, Parent.TrueSystemTypeCode) Then
            _AllowNegative = True
         Else
            _AllowNegative = False
         End If
      End Set
   End Property

   Public Enum MinComparison
      MinLessThanValue
      MinLessThanOrEqualToValue
   End Enum

   Private _MinComparisonType As MinComparison = MinComparison.MinLessThanOrEqualToValue
   <DisplayName("2b. Comparison Type")> _
   <Description("Min < Value or Min <= Value")> _
   <NotifyParentProperty(True)> _
   Public Property MinComparisonType() As MinComparison
      Get
         Return _MinComparisonType
      End Get
      Set(ByVal value As MinComparison)
         _MinComparisonType = value
      End Set
   End Property 'MinComparison

   <NotifyParentProperty(True)> _
   <EditorAttribute("TnTinMn.NIB.Design.NIB_RangeCheckMinMaxPropertiesEditor, TnTinMn.NIB.Design, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", GetType(System.Drawing.Design.UITypeEditor))> _
   <DisplayName("3a. Max Value")> _
   <Description("Maximum allowed value.")> _
   Public Property MaxValue() As ValueType
      Get
         Return _MaxValue
      End Get
      Set(ByVal value As ValueType)
         If value Is Nothing Then value = CType(Convert.ChangeType(0, Parent.SystemType), ValueType)
         MaxOrMinValueRangeCheck(value, Parent.Name)
         _MaxValue = value
      End Set
   End Property

   Public Enum MaxComparison
      ValueLessThanMax
      ValueLessThanOrEqualToMax
   End Enum


   Private _MaxComparisonType As MaxComparison = MaxComparison.ValueLessThanOrEqualToMax
   <DisplayName("3b. Comparison Type")> _
   <Description("value < Max or value <= Max")> _
   <NotifyParentProperty(True)> _
   Public Property MaxComparisonType() As MaxComparison
      Get
         Return _MaxComparisonType
      End Get
      Set(ByVal value As MaxComparison)
         _MaxComparisonType = value
      End Set
   End Property 'MaxComparison

   ''' <summary>
   ''' range checks Max or Min against systemtype max or min
   ''' </summary>
   Private Sub MaxOrMinValueRangeCheck( _
                                 ByRef Min_or_Max As ValueType, _
                                 ByVal PropName As String)

      Dim value As ValueType = Min_or_Max
      Try
         Min_or_Max = CType(Convert.ChangeType(value, Parent.SystemType), ValueType)
      Catch ex As Exception

         If NumericInputBox.A_IsLessThan_B(Min_or_Max, NumericInputBox.GetMin(Parent.SystemType), Parent.TrueSystemTypeCode) Then

            If (Not Parent.IsLayoutSuspended(Parent)) Then _
              MsgBox("Your current " & _
              PropName & _
              " is to small for " & Parent.SystemType.Name & _
              ".  Setting to data type min", , "Variable = " & Parent.ErrorMessageProperties.ErrName)

            Min_or_Max = NumericInputBox.GetMin(Parent.SystemType)
            Exit Sub

         ElseIf NumericInputBox.A_IsGreaterThan_B(Min_or_Max, NumericInputBox.GetMax(Parent.SystemType), Parent.TrueSystemTypeCode) Then

            If (Not Parent.IsLayoutSuspended(Parent)) Then _
            MsgBox("Your " & PropName & _
               " is to large for " & Parent.SystemType.Name & _
               ". Setting to data type max", , "Variable = " & Parent.ErrorMessageProperties.ErrName)
            Min_or_Max = NumericInputBox.GetMax(Parent.SystemType)

         End If
      End Try
   End Sub
End Class 'RangeCheckProperties

